<template>
<div class="auth-page">
  <div class="container page">
    <div class="row">
      <div class="col-md-6 offset-md-3 col-xs-12">
        <h1 class="text-xs-center">Sign up</h1>
        <p class="text-xs-center">
          <RouterLink to="/login">Have an account?</RouterLink>
        </p>

        <ul class="error-messages">
          <li>That email is already taken</li>
        </ul>

        <form>
          <fieldset class="form-group">
            <input class="form-control form-control-lg" type="text" placeholder="Username" v-model="register_req.user.username"/>
          </fieldset>
          <fieldset class="form-group">
            <input class="form-control form-control-lg" type="text" placeholder="Email" v-model="register_req.user.email"/>
          </fieldset>
          <fieldset class="form-group">
            <input class="form-control form-control-lg" type="password" placeholder="Password" v-model="register_req.user.password"/>
          </fieldset>
          <button class="btn btn-lg btn-primary pull-xs-right" @click.prevent="doRegister()">Sign up</button>
        </form>
      </div>
    </div>
  </div>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import axios from '../utils/request';
import type { LoginRsp, RegisterReq } from '../models';
import { useUserStore } from '../store/user'
import router from '../router';

let store = useUserStore();
let { setUserInfo } = store;
let register_req = ref<RegisterReq>({
  user: {
    username: '',
    email: '',
    password: ''
  }
})
let register_rsp = ref<LoginRsp>({
    user: {
        email: '',
        token: '',
        username: '',
        bio: ''
    }
})
function doRegister(){
      axios.post('/users', register_req.value).then(res => {
        register_rsp.value = res.data;
        setUserInfo(register_rsp.value.user)
        router.push({name:'home'})
        // alert(register_rsp.value.user.token)
    })
}
</script>
<style scoped>
</style>
